﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using pinduoduo.Model;
using pinduoduo.Service;
using pinduoduo.Util;
using Sunny.UI;

namespace pinduoduo.Page
{
    public partial class MainAccountPage : UIPage
    {

        private ChromiumWebBrowser AccountChromeBrowser;
        private RequestContext AccountContext;
        private PddTokenService pddTokenService;
        private Main mainForm;
        public MainAccountPage(Main mainForm)
        {
            this.mainForm = mainForm;
            InitializeComponent();
            pddTokenService = new PddTokenService();
            InitializeAccountChromium();
        }

        //初始化浏览器并启动
        public void InitializeAccountChromium()
        {

            AccountContext = CefUtils.GetAccountContext();

            AccountChromeBrowser = new ChromiumWebBrowser("http://yangkeduo.com/", AccountContext);

            this.uiPanel1.Controls.Add(AccountChromeBrowser);

            AccountChromeBrowser.Dock = DockStyle.Fill;
        }

        private async void uiButton1_Click(object sender, EventArgs e)
        {
            await AccountContext.GetCookieManager(null).FlushStoreAsync();

            List<CefSharp.Cookie> cookies2 = await AccountContext.GetCookieManager(null).VisitUrlCookiesAsync("http://mobile.yangkeduo.com", true);
            List<CefSharp.Cookie> cookies = await AccountContext.GetCookieManager(null).VisitAllCookiesAsync();
            if (cookies == null || cookies.Count == 0)
            {
                ShowErrorDialog("请先登录账号");
                return;
            }

            CefSharp.Cookie PDDAccessTokenCookie = cookies.Where(x => { return x.Name == "PDDAccessToken"; }).FirstOrDefault();
            CefSharp.Cookie pdd_user_id_cookie = cookies.Where(x => { return x.Name == "pdd_user_id"; }).FirstOrDefault();
            if (PDDAccessTokenCookie == null || pdd_user_id_cookie == null)
            {
                ShowErrorDialog("请先登录账号");
                return;
            }
            PddToken pddToken = pddTokenService.GetAllToken().Where(x => x.account_type == 1).FirstOrDefault();
            if (pddToken == null)
            {
                pddToken = new PddToken();
                pddToken.PDDAccessToken = PDDAccessTokenCookie.Value;
                pddToken.pdd_user_id = pdd_user_id_cookie.Value;
                pddToken.account_type = 1;
                long id = pddTokenService.InsertToken(pddToken, true);
                if (id > 0) { ShowSuccessDialog("设置成功"); }
            }
            else
            {
                pddToken.PDDAccessToken = PDDAccessTokenCookie.Value;
                pddToken.pdd_user_id = pdd_user_id_cookie.Value;
                pddToken.account_type = 1;
                bool reslut = pddTokenService.UpdateToken(pddToken, true);
                if (reslut) { ShowSuccessDialog("设置成功"); }

            }
            mainForm.mainPage.AccountPddToken = pddToken;
        }
 
        private  async void uiButton2_Click(object sender, EventArgs e)
        {
            if (ShowAskDialog("您确定退出登录吗", "提示框"))
            {
                await AccountContext.GetCookieManager(null).DeleteCookiesAsync();
            }
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {

            AccountChromeBrowser.Load("http://yangkeduo.com/login.html");
        }
    }
}
